using IQIGame.Onigao.Game;
using System;

namespace IQIGame.Onigao.Logic
{
    public static class LgMathUtil
    {
        /// <summary>
        /// 将long强制转换成int，当精度超出时，尽量保证其边界不会超出预期
        /// </summary>
        /// <returns></returns>
        public static int ToIntInRange(long value)
        {
            if (value > int.MaxValue)
            {
                return int.MaxValue;
            }
            else if (value < int.MinValue)
            {
                return int.MinValue;
            }
            return (int)value;
        }

        /// <summary>
        /// 获取cos值
        /// </summary>
        /// <param name="angleDeg">角度（0-180度）</param>
        /// <param name="cosValue">cos值</param>
        /// <returns>是否成功（该方法只提供了写死的角度值，如果不在此范围内则计算失败）</returns>
        public static bool FastCos(int angleDeg, out float cosValue)
        {
            if (LgMathConst.CosValueMap.TryGetValue(angleDeg, out float lCosValue))
            {
                cosValue = lCosValue;
                return true;
            }
            LogicLog.LogError("逻辑层快速cos计算只支持0-180度中，间隔15度的角度。");
            cosValue = 0f;
            return false;
        }

        public static Vector3Logic RotateOffset(Vector3Logic offset, float rotationY)
        {
            // 将角度转换为弧度
            float radian = rotationY * MathLogic.Deg2Rad;
            // 计算旋转矩阵
            double cos = Math.Cos(radian);
            double sin = Math.Sin(radian);
            // 应用旋转矩阵
            double newX = offset.x * cos + offset.z * sin;
            double newZ = -offset.x * sin + offset.z * cos;
            // 返回旋转后的向量
            return new Vector3Logic((float)newX, offset.y, (float)newZ);
        }
    }
}
